home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / demo / demow32s.zip / SOURCE.ZIP / BMPEF.CPP next >
C/C++ Source or Header  |  1993-08-24  |  17KB  |  464 lines

  1. /****************************************************************************
  2.  
  3.     PROGRAM:
  4.  
  5.     PURPOSE: BmpEf template for Windows applications
  6.  
  7.     FUNCTIONS:
  8.  
  9.         WinMain() - calls initialization function, processes message loop
  10.         InitApplication() - initializes window data and registers window
  11.         InitInstance() - saves instance handle and creates main window
  12.         MainWndProc() - processes messages
  13.         About() - processes messages for "About" dialog box
  14.  
  15.     COMMENTS:
  16.  
  17. ****************************************************************************/
  18.  
  19. #include <windows.h>
  20. #include <windowsx.h>
  21. #include <shellapi.h>
  22. #include <commdlg.h>
  23. #include <string.h>
  24. #include "winx31ad.h"
  25. #include "usect3D.h"
  26.  
  27. #include "gv_tbar.h"
  28. #define HEREGLOBAL
  29. #include "bmpef.h"
  30. #include "statetoo.h"
  31.  
  32. HBITMAP HBitTB;
  33. HWND hWndTB;
  34. HWND hWndDraw;
  35. HWND hWndClient;                                        
  36.  
  37. #define WINDOWMENU  2  /* position of window menu     */
  38.  
  39. /****************************************************************************
  40.  
  41.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  42.  
  43. ****************************************************************************/
  44.  
  45. int PASCAL WinMain(
  46. HANDLE hInstance,                            /* current instance             */
  47. HANDLE hPrevInstance,                        /* previous instance            */
  48. LPSTR lpCmdLine,                             /* command line                 */
  49. int nCmdShow)                                /* show-window type (open/icon) */
  50. {
  51.     MSG msg;                                 /* message                      */
  52.  
  53.     LoadRegistCtlModule(hInstance);
  54.     useCtl3dAutoSubclass(hInstance);
  55.     if (!hPrevInstance)                  /* Other instances of app running? */
  56.         if (!InitApplication(hInstance)) /* Initialize shared things */
  57.             return (FALSE);              /* Exits if unable to initialize     */
  58.  
  59.     /* Perform initializations that apply to a specific instance */
  60.  
  61.     if (!InitInstance(hInstance, nCmdShow))
  62.         return (FALSE);
  63.  
  64.     /* Acquire and dispatch messages until a WM_QUIT message is received. */
  65.  
  66.     while (GetMessage(&msg,        /* message structure                      */
  67.             NULL,                  /* handle of window receiving the message */
  68.             NULL,                  /* lowest message to examine              */
  69.             NULL))                 /* highest message to examine             */
  70.         {
  71.         TranslateMessage(&msg);    /* Translates virtual key codes           */
  72.         DispatchMessage(&msg);     /* Dispatches message to window           */
  73.     }
  74.     UnregTBClass (hInstance);
  75.     useCtl3dUnregister(hInstance);
  76.  
  77.     return (msg.wParam);       /* Returns the value from PostQuitMessage */
  78. }
  79.  
  80.  
  81. /****************************************************************************
  82.  
  83.     FUNCTION: InitApplication(HANDLE)
  84.  
  85.     PURPOSE: Initializes window data and registers window class
  86.  
  87. ****************************************************************************/
  88.  
  89. BOOL InitApplication(HANDLE hInstance)
  90. {
  91.     WNDCLASS  wc,wc2;
  92.  
  93.     /* Fill in window class structure with parameters that describe the       */
  94.     /* main window.                                                           */
  95.  
  96.     wc.style = 0 ; //CS_HREDRAW | CS_VREDRAW;           
  97.     wc.lpfnWndProc = MainWndProc;       
  98.                                       
  99.     wc.cbClsExtra = 0;                  /* No per-class extra data.           */
  100.     wc.cbWndExtra = 0;                  /* No per-window extra data.          */
  101.     wc.hInstance = hInstance;           /* Application that owns the class.   */
  102.     wc.hIcon = LoadIcon(hInstance,"BMPEFICON");
  103.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  104.     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  105.     wc.lpszMenuName =  "BMPEFMENU";   /* Name of menu resource in .RC file. */
  106.     wc.lpszClassName = "BmpEfWClass"; /* Name used in call to CreateWindow. */
  107.                     
  108.     wc2.style = 0 ; //CS_HREDRAW | CS_VREDRAW;
  109.     
  110.     wc2.lpfnWndProc = DrawWndProc;
  111.  
  112.     wc2.cbClsExtra = 0;
  113.     wc2.cbWndExtra = sizeof(LPVOID);
  114.     wc2.hInstance = hInstance;
  115.     wc2.hIcon = NULL;
  116.     wc2.hCursor = LoadCursor(NULL, IDC_ARROW);
  117.     wc2.hbrBackground = GetStockObject(LTGRAY_BRUSH);
  118.     wc2.lpszMenuName =NULL ;
  119.     wc2.lpszClassName = "BmpEfWDrawClass";
  120.                     
  121.     /* Register the window class and return success/failure code. */
  122.  
  123.     wc2.style = wc2.style | CS_OWNDC;
  124.  
  125.     return (RegisterClass(&wc) && RegisterTBClass(hInstance) && RegisterClass(&wc2));
  126.  
  127. }
  128.  
  129.  
  130. /****************************************************************************
  131.  
  132.     FUNCTION:  InitInstance(HANDLE, int)
  133.  
  134.     PURPOSE:  Saves instance handle and creates main window
  135.  
  136.     COMMENTS:
  137.  
  138.         This function is called at initialization time for every instance of
  139.         this application.
  140.  
  141. ****************************************************************************/
  142.  
  143. BOOL InitInstance(
  144.     HANDLE          hInstance,          /* Current instance identifier.       */
  145.     int             nCmdShow)           /* Param for first ShowWindow() call. */
  146. {
  147.     HWND            hWnd;               /* Main window handle.                */
  148.  
  149.     ghInst = hInstance;
  150.  
  151.     HBitTB = LoadBitmap (ghInst,"TOOLBMP") ;
  152.  
  153.  
  154.     /* Create a main window for this application instance.  */
  155.  
  156.     hWnd = CreateWindow(
  157.         "BmpEfWClass",                /* See RegisterClass() call.          */
  158.         "BmpEf Sample Application",   /* Text for window title bar.         */
  159.         WS_OVERLAPPEDWINDOW,            /* Window style.                      */
  160.         CW_USEDEFAULT,                  /* Default horizontal position.       */
  161.         CW_USEDEFAULT,                  /* Default vertical position.         */
  162.         CW_USEDEFAULT,                  /* Default width.                     */
  163.         CW_USEDEFAULT,                  /* Default height.                    */
  164.         NULL,                           /* Overlapped windows have no parent. */
  165.         NULL,                           /* Use the window class menu.         */
  166.         hInstance,                      /* This instance owns this window.    */
  167.         NULL                            /* Pointer not needed.                */
  168.     );
  169.  
  170.     /* If window could not be created, return "failure" */
  171.  
  172.     if (!hWnd)
  173.         return (FALSE);
  174.  
  175.     /* Make the window visible; update its client area; and return "success" */
  176.  
  177.     ShowWindow(hWnd, nCmdShow);  /* Show the window                        */
  178.     UpdateWindow(hWnd);          /* Sends WM_PAINT message                 */
  179.     return (TRUE);               /* Returns the value from PostQuitMessage */
  180. }
  181.  
  182. /****************************************************************************
  183.  
  184.     FUNCTION: MainWndProc(HWND, UINT, WPARAM, LPARAM)
  185.  
  186. ****************************************************************************/
  187.  
  188. HWND CreateChild(LPSTR lpFn)
  189. {
  190. MDICREATESTRUCT mcs;
  191.   mcs.szClass = "BmpEfWDrawClass";
  192.   mcs.szTitle = "BmpEf";
  193.   mcs.hOwner =  ghInst;
  194.   mcs.x =       CW_USEDEFAULT;
  195.   mcs.y =       CW_USEDEFAULT;
  196.   mcs.cx =      CW_USEDEFAULT;
  197.   mcs.cy =      CW_USEDEFAULT;
  198.   mcs.style =   0 ;
  199.   mcs.lParam =  (LPARAM)lpFn;
  200.   return (HWND)SendMessage(hWndClient,WM_MDICREATE,0,
  201.             (LPARAM)((LPMDICREATESTRUCT)&mcs));
  202. }                          
  203.  
  204. BOOL DoOpen(HWND hWnd)
  205. {
  206. char szFileTitle[256];
  207. OPENFILENAME ofn;
  208. char szFilter [256] ;
  209. char szExt[]="BMP";
  210. int i,iLnsz; 
  211. LPSTR lpszFile;
  212. LPSTR lpszPosSpace,lpszNextSpace;
  213. char szFullName[256];
  214.  
  215. #define SIZEBUFOPEN 32700
  216.                
  217.    if ((lpszFile = (LPSTR)GlobalAllocPtr(GHND,SIZEBUFOPEN+2))==NULL) return FALSE;
  218.    LoadString(ghInst,IDS_TYPECDLG,szFilter,sizeof(szFilter)-1);
  219.    iLnsz=lstrlen(szFilter);
  220.    for (i=0;i<iLnsz;i++)
  221.      if (szFilter[i] == '|') szFilter[i] = '\0';
  222.  
  223.    _fmemset(&ofn, 0, sizeof(OPENFILENAME));
  224.  
  225.    ofn.lStructSize = sizeof(OPENFILENAME);
  226.    ofn.hwndOwner = hWnd;
  227.    ofn.lpstrFilter = (LPSTR)szFilter ;
  228.    ofn.nFilterIndex = 1;
  229.    ofn.lpstrFile = lpszFile;
  230.    ofn.nMaxFile = SIZEBUFOPEN;
  231.    ofn.lpstrDefExt=szExt;
  232.  
  233.    ofn.lpstrFileTitle = szFileTitle;
  234.    ofn.nMaxFileTitle = sizeof(szFileTitle);
  235.    //ofn.lpstrInitialDir = szDirName;
  236.  
  237.    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT;
  238.    if (GetOpenFileName(&ofn)==FALSE) 
  239.      {
  240.        GlobalFreePtr(lpszFile);
  241.        return FALSE;
  242.      }        
  243.      
  244.      
  245.     SetCursor(LoadCursor(NULL,IDC_WAIT));
  246.     if ((lpszPosSpace=_fstrchr(lpszFile,(int)' '))==NULL)
  247.       {
  248.           if (!CreateChild(lpszFile))
  249.             {
  250.               //wsprintf(szMsg,szErr,lpszFile);
  251.               //MessageBox(hWnd,szMsg,NULL,MB_OK);
  252.             }
  253.            //else fDirty = TRUE;
  254.       }
  255.      else
  256.       {
  257.           *lpszPosSpace='\0';
  258.           if (*(lpszFile+lstrlen(lpszFile)-1)=='\\')
  259.           *(lpszFile+lstrlen(lpszFile)-1)='\0';
  260.           do
  261.           {
  262.             lpszNextSpace=_fstrchr(lpszPosSpace+1,(int)' ');
  263.             if (lpszNextSpace!=NULL) *lpszNextSpace='\0';
  264.             lstrcpy(szFullName,lpszFile);
  265.             lstrcat(szFullName,"\\");
  266.             lstrcat(szFullName,lpszPosSpace+1);
  267.             if (!CreateChild(szFullName))
  268.               {
  269.                  //wsprintf(szMsg,szErr,(LPSTR)szFullName);
  270.                  //if (MessageBox(hWnd,szMsg,NULL,MB_OKCANCEL)==IDCANCEL)
  271.                  //  {
  272.                  //    break;
  273.                  //  }
  274.               }
  275.              //else fDirty = TRUE;
  276.           }
  277.       while ((lpszPosSpace=lpszNextSpace)!=NULL);
  278.       }
  279.   SetCursor(LoadCursor(NULL,IDC_ARROW));
  280.  
  281.   return TRUE;
  282. }
  283.  
  284. long CALLBACK __export MainWndProc(
  285. HWND hWnd,                                /* window handle                   */
  286. UINT message,                         /* type of message                 */
  287. WPARAM wParam,                              /* additional information          */
  288. LPARAM lParam)                              /* additional information          */
  289. {
  290.     RECT rect;
  291.     CLIENTCREATESTRUCT clientCreate;
  292.     switch (message) {
  293.     case WM_CREATE:
  294.             #ifdef WIN32
  295.             SetWindowText(hWnd,"BMPEF32 - (c) Gilles VOLLANT");
  296.             #else
  297.             SetWindowText(hWnd,"BMPEF16 - (c) Gilles VOLLANT");
  298.             #endif
  299.             GetClientRect(hWnd,&rect);
  300.             rect.bottom=SIZEY;
  301.  
  302.             hWndTB=CreateTB(hWnd,ghInst,HBitTB,rect);
  303.             
  304.             AddBtn(hWndTB,IDM_NEW,0,BSTY_ACTION,0,0,0,0,0);
  305.             AddBtn(hWndTB,IDM_OPEN,1,BSTY_ACTION,0,0,0,0,0);
  306.             AddBtn(hWndTB,IDM_SAVEAS,2,BSTY_ACTION,0,0,0,0,0);
  307.             AddBtn(hWndTB,IDM_ROTATE,9,BSTY_ACTION,0,0,0,0,0);
  308.             //AddBtn(hWndTB,IDM_SETUPPRN,9,BSTY_ACTION,0,0,0,0,0);
  309.             //AddBtn(hWndTB,IDM_COPY   ,4,BSTY_ACTION,0,0,0,0,0);
  310.             //AddBtn(hWndTB,IDM_PRINT   ,9,BSTY_ACTION,0,0,0,0,0);
  311.             //AddBtn(hWndTB,IDM_OPENPARAMBOX,6,BSTY_ACTION,0,0*5,0,0,0);
  312.             DoSetTextInfo(" ");
  313.                                   
  314.             clientCreate.hWindowMenu = GetSubMenu (GetMenu(hWnd),WINDOWMENU);
  315.             clientCreate.idFirstChild = IDM_FIRSTCHILD;
  316.             hWndClient = CreateWindow(
  317.                           "MDICLIENT",
  318.                           NULL,
  319.                           WS_VISIBLE | WS_CHILD | WS_CLIPCHILDREN,
  320.                           CW_USEDEFAULT,
  321.                           CW_USEDEFAULT,
  322.                           CW_USEDEFAULT,
  323.                           CW_USEDEFAULT,
  324.                           hWnd,
  325.                           0,
  326.                           ghInst,
  327.                           (LPSTR)&clientCreate
  328.                           );
  329.                           
  330.             DragAcceptFiles(hWnd,TRUE);
  331.             break;
  332.  
  333.         case WM_SIZE:
  334.             GetClientRect(hWnd,&rect);
  335.             SetWindowPos(hWndTB,NULL,0,0,rect.right,SIZEY,SWP_NOZORDER);  
  336.             SetWindowPos(hWndClient,NULL,0,SIZEY,rect.right,rect.bottom-(2*SIZEY),SWP_NOZORDER);  
  337.             rect.top = rect.bottom - SIZEY;
  338.             DoSetInfo(hWnd,&rect);
  339.             return NULL;
  340.             
  341.         case WM_PAINT:    
  342.             {
  343.             HDC hDC;
  344.             PAINTSTRUCT ps;
  345.               hDC = BeginPaint(hWnd,&ps);
  346.               DoPaintInfo(hDC);          
  347.               EndPaint(hWnd,&ps);
  348.               break;
  349.             }          
  350.             
  351.         case WM_DROPFILES:
  352.           {
  353.           HANDLE hDrop = (HANDLE)wParam;
  354.           UINT uiNbFile,i;
  355.           char szFn[128];
  356.             uiNbFile = DragQueryFile(hDrop,(UINT)-1,0,0);
  357.             for (i=0;i<uiNbFile;i++)
  358.               {
  359.                 DragQueryFile(hDrop,i,szFn,sizeof(szFn)-1);
  360.                 CreateChild(szFn);
  361.               } 
  362.             DragFinish(hDrop);
  363.             break;  
  364.           }    
  365.           
  366.         case WM_COMMAND:           /* message: command from application menu */
  367.           switch (GET_WM_COMMAND_ID(wParam,lParam))
  368.           {                 
  369.                 case IDM_TILE:
  370.                   SendMessage(hWndClient,WM_MDITILE,0,0);
  371.                   return 0;
  372.                   
  373.                 case IDM_CASCADE:
  374.                   SendMessage(hWndClient,WM_MDICASCADE,0,0);
  375.                   return 0;
  376.                   
  377.                 case IDM_ARRANGE:
  378.                   SendMessage(hWndClient,WM_MDIICONARRANGE,0,0);
  379.                   return 0;
  380.                   
  381.                 case IDM_OPEN:
  382.                   DoOpen(hWnd);
  383.                   break;      
  384.                 
  385.                 case IDM_ABOUT :
  386.                     DialogBox(ghInst,        /* current instance         */
  387.                       "AboutBox",                  /* resource to use          */
  388.                       hWnd,                        /* parent handle            */
  389.                       (DLGPROC)About);                /* About() instance address */
  390.                 break;
  391.  
  392.                 case IDM_EXIT:
  393.                   SendMessage(hWnd,WM_CLOSE,0,0);
  394.                   break;
  395.                 
  396.                 default:      
  397.                   {
  398.                   HWND hWndChild;
  399.                     hWndChild = (HWND)SendMessage(hWndClient,WM_MDIGETACTIVE,0,0);
  400.                     if (IsWindow(hWndChild))
  401.                       SendMessage(hWndChild,message,wParam,lParam);
  402.                   }             
  403.           }
  404.         break;
  405.         
  406.         case WM_ACTIVATE :
  407.         case WM_QUERYNEWPALETTE:
  408.                   {
  409.                   HWND hWndChild;
  410.                     hWndChild = (HWND)SendMessage(hWndClient,WM_MDIGETACTIVE,0,0);
  411.                     if (IsWindow(hWndChild))
  412.                       return SendMessage(hWndChild,message,wParam,lParam);
  413.                     break;  
  414.                   }             
  415.  
  416.         case WM_DESTROY:                  /* message: window being destroyed */
  417.             PostQuitMessage(0);
  418.             break;
  419.     }
  420.     return (DefFrameProc(hWnd,hWndClient, message, wParam, lParam));
  421. }
  422.  
  423.  
  424. /****************************************************************************
  425.  
  426.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  427.  
  428.     PURPOSE:  Processes messages for "About" dialog box
  429.  
  430.     MESSAGES:
  431.  
  432.         WM_INITDIALOG - initialize dialog box
  433.         WM_COMMAND    - Input received
  434.  
  435.     COMMENTS:
  436.  
  437.         No initialization is needed for this particular dialog box, but TRUE
  438.         must be returned to Windows.
  439.  
  440.         Wait for user to click on "Ok" button, then close the dialog box.
  441.  
  442. ****************************************************************************/
  443.  
  444. BOOL CALLBACK __export About(
  445. HWND hDlg,                                /* window handle of the dialog box */
  446. unsigned message,                         /* type of message                 */
  447. WORD wParam,                              /* message-specific information    */
  448. LONG lParam)
  449. {
  450.     switch (message) {
  451.         case WM_INITDIALOG:                /* message: initialize dialog box */
  452.             return (TRUE);
  453.  
  454.         case WM_COMMAND:                      /* message: received a command */
  455.             if (wParam == IDOK                /* "OK" box selected?          */
  456.                 || wParam == IDCANCEL) {      /* System menu close command? */
  457.                 EndDialog(hDlg, TRUE);        /* Exits the dialog box        */
  458.                 return (TRUE);
  459.             }
  460.             break;
  461.     }
  462.     return (FALSE);                           /* Didn't process a message    */
  463. }
  464.